package com.china.zhuoyue.ui.fragment

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.fragment.app.activityViewModels
import com.china.zhuoyue.R
import com.china.zhuoyue.bean.MineFun
import com.china.zhuoyue.databinding.FragemntMineBinding
import com.china.zhuoyue.databinding.ItemMineServiceBinding
import com.china.zhuoyue.ui.activity.commom.FeedbackActivity
import com.china.zhuoyue.ui.activity.commom.SettingActivity
import com.china.zhuoyue.ui.activity.estate.SmartStopCarActivity
import com.china.zhuoyue.ui.activity.estate.home.service.*
import com.china.zhuoyue.ui.activity.market.MarketMainActivity
import com.china.zhuoyue.ui.activity.user.AddAuthActivity
import com.china.zhuoyue.ui.activity.user.CarAuthActivity
import com.china.zhuoyue.ui.activity.user.InviteFriendsActivity
import com.china.zhuoyue.ui.activity.user.UserCenterActivity
import com.china.zhuoyue.ui.base.BaseFragment
import com.china.zhuoyue.ui.base.BaseRecyclerAdapter
import com.china.zhuoyue.ui.base.BaseViewHolder
import com.china.zhuoyue.ui.cache.UserCache
import com.china.zhuoyue.ui.ext.startActivityExt
import com.china.zhuoyue.ui.utils.FullyGridLayoutManager
import com.china.zhuoyue.ui.utils.GlideUtils
import com.china.zhuoyue.ui.viewmodel.MainViewModel


class MineFragment : BaseFragment<FragemntMineBinding>() {
    private val mMainViewModel: MainViewModel by activityViewModels()
    private val mPersonalService = MineServiceAdapter()
    private val mSocialService = MineServiceAdapter()

    override fun initView(rootView: View?, savedInstanceState: Bundle?) {
        loadView {
            bindViewClick(ivHead, textMineHelpFeedback, textMineContactService, textMineSetting)
        }
        mPersonalService.datas = MineServiceAdapter.PERSONAL_SERVICE
        mPersonalService.setOnClickAdapterListenter(::onClickPersonalService)
        vb.rvPersonalService.adapter = mPersonalService
        vb.rvPersonalService.layoutManager = FullyGridLayoutManager(requireContext(), 4)

        mSocialService.setOnClickAdapterListenter(::onClickSocialService)
        mSocialService.datas = MineServiceAdapter.SOCIAL_SERVICE
        vb.rvSocialService.adapter = mSocialService
        vb.rvSocialService.layoutManager = FullyGridLayoutManager(requireContext(), 4)
    }

    override fun initData(rootView: View?, savedInstanceState: Bundle?) {
        UserCache.getUserInfoBean().apply {
            GlideUtils.intoView(mThis, avatar, vb.ivHead, placeholder = R.mipmap.icon_mine_default_head)
            vb.tvNickName.text = nickname
        }
        vb.tvLoc.text = MainViewModel.DEFAULT_HOME
        mMainViewModel.changeHomeLiveData.observe(this) {
            vb.tvLoc.text = it
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.ivHead -> startActivityExt<UserCenterActivity>()
            vb.textMineSetting -> startActivityExt<SettingActivity>()
            vb.textMineHelpFeedback -> startActivityExt<FeedbackActivity>()
            vb.textMineContactService -> openCallPhone("0755-88358041")
        }
    }

    private fun onClickPersonalService(v: View?, position: Int) {
        when (mPersonalService.getItem(position).nameResId) {
            R.string.text_mine_personal_service_fun_1 -> startActivityExt<SmartStopCarActivity>()
            R.string.text_mine_personal_service_fun_2 -> startActivityExt<EstatePaymentActivity>()
            R.string.text_mine_personal_service_fun_3 -> startActivityExt<GoodsPassActivity>()
            R.string.text_mine_personal_service_fun_4 -> startActivityExt<DecorateApplyActivity>()
            R.string.text_mine_personal_service_fun_5 -> startActivityExt<RepairActivity>()
            R.string.text_mine_personal_service_fun_6 -> startActivityExt<FeedbackActivity>()
            R.string.text_mine_personal_service_fun_7 -> startActivityExt<ScoreActivity>()
            R.string.text_mine_personal_service_fun_8 -> startActivityExt<CommunityNoticeActivity>()
        }
    }

    private fun onClickSocialService(v: View?, position: Int) {
        when (mSocialService.getItem(position).nameResId) {
            R.string.text_mine_social_service_fun_1 -> startActivityExt<MarketMainActivity>()
            R.string.text_mine_social_service_fun_2 -> startActivityExt<AddAuthActivity>()
            R.string.text_mine_social_service_fun_3 -> startActivityExt<CarAuthActivity>()
            R.string.text_mine_social_service_fun_4 -> openCallPhone("0755-88358041")
            R.string.text_mine_social_service_fun_5 -> startActivityExt<InviteFriendsActivity>()
            R.string.text_mine_social_service_fun_6 -> startActivityExt<CommunityNoticeActivity>()
        }
    }

    private fun openCallPhone(phone: String) {
        val intent = Intent()
        intent.action = Intent.ACTION_DIAL
        intent.data = Uri.parse("tel:$phone")
        startActivity(intent)
    }
}

class MineServiceAdapter : BaseRecyclerAdapter<BaseViewHolder<ItemMineServiceBinding>, MineFun>() {
    companion object {
        val PERSONAL_SERVICE = mutableListOf<MineFun>().apply {
            add(MineFun(R.string.text_mine_personal_service_fun_1, R.mipmap.icon_mine_personal_service_fun_1))
            add(MineFun(R.string.text_mine_personal_service_fun_2, R.mipmap.icon_mine_personal_service_fun_2))
            add(MineFun(R.string.text_mine_personal_service_fun_3, R.mipmap.icon_mine_personal_service_fun_3))
            add(MineFun(R.string.text_mine_personal_service_fun_4, R.mipmap.icon_mine_personal_service_fun_4))
            add(MineFun(R.string.text_mine_personal_service_fun_5, R.mipmap.icon_mine_personal_service_fun_5))
            add(MineFun(R.string.text_mine_personal_service_fun_6, R.mipmap.icon_mine_personal_service_fun_6))
            add(MineFun(R.string.text_mine_personal_service_fun_7, R.mipmap.icon_mine_personal_service_fun_7))
            add(MineFun(R.string.text_mine_personal_service_fun_8, R.mipmap.icon_mine_personal_service_fun_8))
        }
        val SOCIAL_SERVICE = mutableListOf<MineFun>().apply {
            add(MineFun(R.string.text_mine_social_service_fun_1, R.mipmap.icon_mine_social_service_fun_1))
            add(MineFun(R.string.text_mine_social_service_fun_2, R.mipmap.icon_mine_social_service_fun_2))
            add(MineFun(R.string.text_mine_social_service_fun_3, R.mipmap.icon_mine_social_service_fun_3))
            add(MineFun(R.string.text_mine_social_service_fun_4, R.mipmap.icon_mine_social_service_fun_4))
            add(MineFun(R.string.text_mine_social_service_fun_5, R.mipmap.icon_mine_social_service_fun_5))
            add(MineFun(R.string.text_mine_social_service_fun_6, R.mipmap.icon_mine_social_service_fun_6))
        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder<ItemMineServiceBinding>, bean: MineFun, position: Int) {
        holder.vb.apply {
            bean.apply {
                ivImage.setImageResource(bean.iconResId)
                tvName.setText(bean.nameResId)
                setViewClick(position, root)
            }
        }
    }
}